@echo off
chcp 65001 >nul 2>&1
echo ==============================================
echo Onpharos Platform Startup Script
echo ==============================================

:: Check if Docker is running
docker info >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: Docker is not running. Please start Docker Desktop first.
    pause
    exit /b 1
) else (
    echo [OK] Docker: Running normally
)

:: Check if WSL2 is enabled
wsl --status >nul 2>&1
if %errorlevel% equ 0 (
    echo [OK] WSL2: Enabled normally
) else (
    echo Error: WSL2 is not detected. Please install WSL2 first.
    pause
    exit /b 1
)

:: Check if NVIDIA Container support works
docker run --rm --gpus all nvidia/cuda:12.8.1-runtime-ubuntu22.04 nvidia-smi >nul 2>&1
if %errorlevel% equ 0 (
    echo [OK] NVIDIA Container: Working normally
) else (
    echo Error: NVIDIA Container support not detected.
    pause
    exit /b 1
)

:: Skip mirror check to avoid block
echo [OK] Docker Domestic Mirror: Check skipped (proceeding)

:: Create required data directories (with explicit logs)
echo Creating required data directories...
set "BASE_DIR=%~dp0"
echo Using base directory: %BASE_DIR%
mkdir "%BASE_DIR%training_data" 2>nul && echo Created training_data || echo training_data already exists
mkdir "%BASE_DIR%models" 2>nul && echo Created models || echo models already exists
mkdir "%BASE_DIR%game_config" 2>nul && echo Created game_config || echo game_config already exists
mkdir "%BASE_DIR%game_logs" 2>nul && echo Created game_logs || echo game_logs already exists
mkdir "%BASE_DIR%src" 2>nul && echo Created src || echo src already exists
mkdir "%BASE_DIR%build" 2>nul && echo Created build || echo build already exists
mkdir "%BASE_DIR%visualizations" 2>nul && echo Created visualizations || echo visualizations already exists
echo [OK] Data directories processed

:: Start Docker containers
echo Starting Onpharos Platform containers...
cd /d "%BASE_DIR%"
docker-compose up --build -d

:: Show completion message
echo.
echo ==============================================
echo Startup Completed Successfully!
echo.
echo Service Access Addresses:
echo - Training Service API: http://localhost:8000
echo - Game Engine API: http://localhost:8001
echo - Code Generator API: http://localhost:8002
echo - Visualization Interface: http://localhost:3000
echo - SSH Access (Training Container): ssh root@localhost -p 2222  (Password: onpharos)
echo ==============================================
echo.
pause